/**
 * Created by apple on 2018/1/9.
 */
var DBFactory = require('../db/mysql_db');
var async = require('async');
var AlipayInfoDao = module.exports;
var Global = require('../global_var');
var Config = require('../config');
var userAgentDao = require('../dao/user_agent');
var UserFreezeMoneyDao = require('../dao/user_freeze_money');


AlipayInfoDao.saveAlipayUserId = function (data,cb) {

    if (!data) return cb(new Error(500));
    DBFactory.getConnection(function (error, connection) {
        if (error) {
            cb(error);
            return;
        }
        var sql1 = 'SELECT ali_user_id FROM user_app_exinfo WHERE uid = ? AND app_sid = ? ';
        connection.query(sql1, [data.uid,data.app_sid], function (err, result) {
            if (err) {
                cb(err);
                connection.release();
                return;
            }
            var userInfo = ( result && result.length > 0 ) ? result[0] : undefined;
            if (userInfo && userInfo.ali_user_id == data.user_id) {
                connection.release();
                return cb(null,'授权成功!');
            }

            var sql2 = 'SELECT uid FROM user_app_exinfo WHERE app_sid = ? AND ali_user_id = ? ';
            connection.query(sql2, [data.app_sid,data.user_id], function (err, result) {
                if (err) {
                    cb(err);
                    connection.release();
                    return;
                }
                var uidInfo = ( result && result.length > 0 ) ? result[0] : undefined;
                if (uidInfo) {
                    connection.release();
                    return cb('该支付宝已授权其他用户，请您更换后再试哦～');
                }
                var sql3 = 'UPDATE user_app_exinfo SET ali_user_id = ? WHERE uid = ? AND app_sid = ? ';
                connection.query(sql3, [data.user_id,data.uid,data.app_sid], function (err, result) {
                    if (err) {
                        cb(err);
                        connection.release();
                        return;
                    }
                    connection.release();
                    cb(null,'授权成功!');
                    if (userInfo && !userInfo.ali_user_id) {
                        userAgentDao.addRealSub(data,'ali_user_id');
                        unfreezeMoneyZFB(data);
                    } //第一次设置阿里账号时，增加有效用户数，方法里会判断是否有uuid
                });
            });
        });
    });
}

function unfreezeMoneyZFB(data) {
    UserFreezeMoneyDao.unfreezeMoney({uid:data.uid,app_sid:data.app_sid,unfreeze_sid:'zfbsq'},function (err,result) {
        if(err){
            console.log(err);
        }
    })
}

AlipayInfoDao.saveAlipayInfo = function (data,cb) {
    if (!data) return cb(new Error(500));
    var sql = 'REPLACE INTO alipay_user_info SET ? ';
    DBFactory.poolQuery(sql, data,
        function () {
            cb('no collection');
        },
        function (err, result) {
            if (err) {
                console.log(err);
                cb(err);
                return;
            }
            cb(null,result);
        }
    );
}

AlipayInfoDao.getAliUserId = function(data, cb) {
    if (!data) return cb(new Error(500));
    var sql = 'select ali_user_id,tb_user_id from user_app_exInfo where uid = ? and app_sid = ? ';
    DBFactory.poolQuery(sql, [data.uid, data.app_sid],
        function () {
            cb('no collection');
        },
        function (err, result) {
            if (err) {
                console.log(err);
                cb(err);
                return;
            }
            if(result && result[0]) {
                cb(null, result[0])
            } else {
                cb(null, {});
            }
        }
    );
}


AlipayInfoDao.firstAuthorizeTB = function (data,cb) {
    if (!data) return cb(new Error(500));
    var sql = 'SELECT tb_user_id FROM user_app_exinfo WHERE uid = ? AND app_sid = ? ';
    DBFactory.getConnection(function (error, connection) {
        if (error) {
            cb(error);
            return;
        }
        connection.query(sql, [data.uid,data.app_sid], function (err, result) {
            if (err) {
                cb(err);
                connection.release();
                return;
            }
            var userInfo = result && result[0] ? result[0] : undefined;
            if (userInfo && userInfo.tb_user_id){
                connection.release();
                cb(null,{tb_user_id:1});
                return;
            }
            var sql1 = 'UPDATE user_app_exinfo SET tb_user_id = 1 WHERE uid = ? AND app_sid = ? ';
            connection.query(sql1, [data.uid,data.app_sid], function (err, result) {
                if (err) {
                    cb(err);
                    connection.release();
                    return;
                }
                connection.release();
                cb(null,{tb_user_id:1});
            });
        });
    });
}


